import scrapy
from scrapy import Request
from demo2.items import fanqieItem
#scrapy crawl fanqie -o fanqie.csv

#div = response.css("#app > div > div.muye.muye-rank > div > div > div.muye-rank-wrap > div.muye-rank-book-list > div")


class FanqieSpider(scrapy.Spider):
    name = "fanqie"
    allowed_domains = ["fanqienovel.com"]
    # start_urls = ["https://fanqienovel.com/rank/"]
    temp = {}

    def start_requests(self):
        yield Request(url='https://fanqienovel.com/api/rank/category/list?app_id=1967&rank_list_type=3&offset=0&limit=20&category_id=8&rank_version&gender=0&rankMold=2'
                      ,meta={'no':0})

    def parse(self, response):
        # open("list.html", "wb").write(response.body).close()
        no = response.meta.get('no')
        # print("\n\n\n\n\n\n\n\n\n\n"+str(no)+'\n\n\n\n\n')
        if no == 0:
            res = response.json()
            books = res['data']['book_list']
            for i in range(1,21):
                book = books[i-1]
                bookid = book['bookId']
                self.temp[i] = {'rank':book['currentPos'],'creation_status':book['creationStatus'],
                                'read_count':book['read_count'],'word_count':book['wordNumber']}
                yield Request(url=f'https://fanqienovel.com/page/{bookid}?enter_from=Rank',meta={'no':i})

        else:
            item = fanqieItem()
            dic = self.temp[no]
            item['rank'] = dic['rank']
            item['creation_status'] = dic['creation_status']
            item['read_count'] = dic['read_count']
            item['word_count'] = dic['word_count']
            res = response.css(
                '#app > div > div.muye.muye-page > div > div.page-wrap > div > div.page-header-info > div.info')
            item['title'] = res.css('.info-name h1::text').get().strip()
            item['genres'] = ' '.join(res.css('.info-label span::text').getall())
            item['last_update_title'] = res.css('.info-last-title::text').getall()[1]
            item['last_update_time'] = res.css('.info-last-time::text').get().strip()
            item['author'] = (response.css(
                '#app > div > div.muye.muye-page > div > div.page-wrap > div > div.page-header-info > div.author > div.author-info > a > div > span.author-name-text::text')
                              .get().strip())
            item['abstract'] = response.css('#app > div > div.muye.muye-page > div > div.page-body-wrap > div > div.page-abstract-content > p::text').get().strip()
            yield item



